package com.jsits.v2x.hurys.controller;

import com.alibaba.fastjson.JSON;
import com.jsits.v2x.common.dto.DetectDto;
import com.jsits.v2x.hurys.config.HurysConfiguration;
import com.jsits.v2x.hurys.dto.DetectVehicleDto;
import com.jsits.v2x.hurys.dto.DetectVehicleListDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 慧尔视车辆检测数据接口
 */

@RestController
@RequestMapping("/radarDataCollect")
@Api(tags = "DetectVehicle", description = "车辆检测数据数据管理")
@Slf4j
public class DetectVehicleController {
    @Autowired
    private HurysConfiguration hurysConfiguration;

    // http客户端
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();

    @RequestMapping(value = "/objData", method = RequestMethod.POST)
    @ApiOperation("车辆检测数据推送")
    public void pushDetectVehicleData(HttpServletRequest request, @RequestBody DetectVehicleDto detectVehicleDto) throws UnsupportedEncodingException {
        DetectDto detectDto = new DetectDto();
        String deviceNo = detectVehicleDto.getDeviceNo();
        detectDto.setDevicePosition(hurysConfiguration.getPosition(deviceNo));
        Integer speed = detectVehicleDto.getSpeed();

        detectDto.setSpeed(speed);
        // TODO
    	log.debug("microwaveData maxSpeedList :  {},{}", detectVehicleDto.conventMaxSpeedList(), detectVehicleDto.getTimestamp());
        log.debug("microwaveData : no:{} speed:{} time:{}",deviceNo,speed,detectVehicleDto.getTimestamp());

        System.out.println(detectDto.getDevicePosition().Value() + ":" + speed);

        //数据转发到计算服务
        // 创建Post请求
        HttpPost httpPost = new HttpPost(hurysConfiguration.getComputeUrl());
        StringEntity entity = new StringEntity(JSON.toJSONString(detectDto), "UTF-8");
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private String conventMaxSpeedList(List<DetectVehicleListDto> list)
    {
    	String result = "";
    	
    	for(DetectVehicleListDto temp : list)
    	{
    		result += " ID : " + temp.getID();
    		result += " length : " + temp.getLength();
    		result += " xPos : " + temp.getXPos();
    		result += " xSpeed : " + temp.getXSpeed();
    		result += " yPos : " + temp.getYPos();
    		result += " ySpeed : " + temp.getYSpeed();
    	}
    	return result;
    }
}
